package com.zxx.xin.shorthand.helper;

import android.annotation.SuppressLint;
import android.util.Log;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by xin on 2018/3/17.
 */

public class DateHelper {

    private static HashMap<String, String> dateChinese = new HashMap<>();
    private static void initDateChinese() {
        if (dateChinese.size() == 0){
            dateChinese.put("0", "零");
            dateChinese.put("1", "一");
            dateChinese.put("2", "二");
            dateChinese.put("3", "三");
            dateChinese.put("4", "四");
            dateChinese.put("5", "五");
            dateChinese.put("6", "六");
            dateChinese.put("7", "七");
            dateChinese.put("8", "八");
            dateChinese.put("9", "九");
            dateChinese.put("10", "十");
        }
    }

    @SuppressLint("WrongConstant")
    private static String parseCalendarToChineseTime(Calendar calendar){
        initDateChinese();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return parseYearToChinese(year) + "年 " + parseMonthToChinese(month) + "月 " + parseDayToChinese(day) + "日";
    }

    private static String parseYearToChinese(int year){
        initDateChinese();
        int first = year / 1000;
        int second = year % 1000 / 100;
        int third = year % 1000 % 100 / 10;
        int four = year % 1000 % 100 % 10;
        return dateChinese.get(first + "") + dateChinese.get(second + "") + dateChinese.get(third + "") + dateChinese.get(four + "");
    }

    private static String parseMonthToChinese(int month){
        initDateChinese();
        StringBuilder sb = new StringBuilder();
        if (month < 10){
            sb.append(dateChinese.get(month + ""));
        } else if(month == 10){
            sb.append(dateChinese.get(10 + ""));
        } else {
            sb.append(dateChinese.get(10 + ""));
            sb.append(dateChinese.get(month % 10 + ""));
        }
        return sb.toString();
    }

    private static String parseDayToChinese(int day){
        initDateChinese();
        StringBuilder sb = new StringBuilder();
        if (day < 10){
            sb.append(dateChinese.get(day + ""));
        } else {
            if (day / 10 > 1){
                sb.append(dateChinese.get(day / 10 + ""));
            }
            sb.append(dateChinese.get(10 + ""));
            if (day % 10 > 0){
                sb.append(dateChinese.get(day % 10 + ""));
            }
        }
        return sb.toString();
    }

    @SuppressLint("WrongConstant")
    public static String getCurrentTime(){
        initDateChinese();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return parseCalendarToChineseTime(calendar);
    }

    public static String getChineseTimeInMillis(long time){
        initDateChinese();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return parseCalendarToChineseTime(calendar);
    }

    @SuppressLint("WrongConstant")
    public static int getMonthInMillis(long time){
        initDateChinese();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return calendar.get(Calendar.MONTH) + 1;
    }

    @SuppressLint("WrongConstant")
    public static String getChineseYearAndMonthInMillis(long time){
        initDateChinese();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        return parseYearToChinese(year) + "年 " + parseMonthToChinese(month) + "月";
    }

    @SuppressLint("WrongConstant")
    public static String getChineseDayInMillis(long time){
        initDateChinese();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        return parseDayToChinese(day) + "日";
    }
}
